package com.adcc.swingui.factory;

import java.awt.Container;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.swing.JDialog;
import javax.swing.JFrame;

import org.apache.log4j.Logger;
import org.hibernate.mapping.Array;

import com.adcc.swingui.base.DefaultDialog;
import com.adcc.swingui.base.DefaultOptionsPanel;
import com.adcc.swingui.base.DefaultPanel;
import com.adcc.swingui.base.SetOptionsDialog;
import com.adcc.swingui.bean.ConfigLayersBean;
import com.adcc.swingui.bean.ConfigLayersNode;
import com.adcc.swingui.conf.SystemSetConf;
import com.adcc.swingui.demo.DemoUi;
import com.adcc.swingui.demo.set.SetUplinkPanel;
import com.jidesoft.dialog.AbstractDialogPage;
import com.jidesoft.dialog.MultiplePageDialog;
import com.jidesoft.dialog.PageList;
import com.jidesoft.docking.DefaultDockableHolder;
import com.jidesoft.plaf.LookAndFeelFactory;
import com.jidesoft.swing.JideSwingUtilities;

/**
 * 系统设置工厂
 * 
 * @author charlie
 * 
 */
public class SetOptionsFactory {
	private static Logger logger = Logger.getLogger(SetOptionsFactory.class);
	private static List<AbstractDialogPage> pages ;

	private static ConfigLayersBean configBean;//系统设置主界面配置元素
	
	
	public static ConfigLayersBean getConfigBean(String configName) {
		configBean = SystemSetConf.getSystemsetConf().get(configName);
		return configBean;
	}

	public static void setConfigBean(ConfigLayersBean confBean) {
		configBean = confBean;
	}

	public static void main(String[] args) {
		SystemSetConf.initConf("conf/default/systemset.xml");
		LookAndFeelFactory.installDefaultLookAndFeelAndExtension();
		create("systemSet1", new DemoUi("xxx"));
	}

	private static void createPanel(List<ConfigLayersNode> nodes,
			AbstractDialogPage parent, Container container) {
		for (int i = 0; i < nodes.size(); i++) {
			ConfigLayersNode node = nodes.get(i);
			AbstractDialogPage panel = null;
			try{
				Class c = Class.forName(node.getPanel());
				Constructor con = c.getConstructor(new Class[] {String.class, Container.class });
				panel = (AbstractDialogPage) con.newInstance(node.getI18nName(), container);
			}catch(Exception e){
				//"属性面板失败"
				logger.error("Create Properties panel failure :" + node.getName() + e.getMessage());
			}
			if(panel == null) {
				continue;
			}
			if (parent != null) {
				panel.setParentPage(parent);
			}
			pages.add(panel);
			if (nodes.get(i).getSubNodes() != null) {
				createPanel(nodes.get(i).getSubNodes(), panel, container);
			}
		}
	}

	@Deprecated
	/**
	 * 创建系统设置对话框（界面操作按钮“确定-取消-应用”文字不可更改）
	 * @param name
	 * @param container
	 */
	public static void create(String name, Container container) {
		ConfigLayersBean bean = SystemSetConf.getSystemsetConf().get(name);
		pages = new ArrayList<AbstractDialogPage>();
		List<ConfigLayersNode> nodes = bean.getNodes();
		createPanel(nodes, null, container);
		final MultiplePageDialog dialog = new SetOptionsDialog((JFrame)container, bean.getI18nName());
		dialog.setStyle(MultiplePageDialog.TREE_STYLE);
		PageList model = new PageList();
		for (AbstractDialogPage page : pages) {
			model.append(page);
		}
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setPageList(model);
		dialog.setInitialPageTitle(bean.getDefaultLayerName());

		dialog.pack();
		JideSwingUtilities.globalCenterWindow(dialog);
		dialog.setVisible(true);
	}
	
	/**
	 * 创建带有初始化界面操作按钮文字的系统设置面板（对话框）
	 * @param container父窗体
	 * @param configBean界面组件封装类
	 * @param actionBtnNames界面操作按钮（确定-取消-应用）
	 */
	public static void create(Container container,ConfigLayersBean configBean,String...actionButtonNames){
		//初始化主机面组件
		if(pages==null){
			pages = new ArrayList<AbstractDialogPage>();
		}else{
			pages.clear();
		}
		createPanel(configBean.getNodes(), null, container);
		//创建对话框
		final MultiplePageDialog dialog = new SetOptionsDialog((JFrame)container, configBean.getI18nName(),actionButtonNames);
		dialog.setStyle(MultiplePageDialog.TREE_STYLE);
		PageList model = new PageList();
		for (AbstractDialogPage page : pages) {
			model.append(page);
		}
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setPageList(model);
		dialog.setInitialPageTitle(configBean.getDefaultLayerName());
		
		dialog.pack();
		JideSwingUtilities.globalCenterWindow(dialog);
		dialog.setVisible(true);
	}
}
